///**
// * Copyright (c) 2015 www.smarthome.com All rights reserved. <br />
// * 本软件源代码版权归xxxx公司,未经许可不得任意复制与传播.<br />
// *
// */
//package com.smarthome.core.account.impl;
//
//import java.util.UUID;
//
//import javax.annotation.Resource;
//
//import org.apache.commons.lang.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.BeanUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import com.smarthome.base.service.ShTestAccountService;
//import com.smarthome.domain.common;
//import com.smarthome.domain.enums.CertStatus;
//import com.smarthome.domain.enums.ResponseCode;
//import com.smarthome.domain.enums.VerifyTokenType;
//import com.smarthome.common.exception.ExceptionUtils;
//import com.smarthome.common.tools.MD5Utils;
//import com.smarthome.common.tools.cache.CacheKeys;
//import com.smarthome.common.tools.cache.JedisUtils;
//import com.smarthome.common.tools.date.DateUtils;
//import com.smarthome.core.account.AccountUpdateService;
//import com.smarthome.core.account.AccountVerifyTokenService;
//import com.smarthome.domain.bean.ShTestAccount;
//import com.smarthome.domain.app.Request;
//import com.smarthome.domain.app.request.account.AccountLoginRequest;
//import com.smarthome.domain.app.request.account.AccountMobileModifyRequest;
//import com.smarthome.domain.app.request.account.AccountPwdResetRequest;
//import com.smarthome.domain.app.request.account.AccountPwdResetVerifyCardRequest;
//import com.smarthome.domain.app.request.account.AccountRegisterRequest;
//import com.smarthome.domain.app.request.sms.SmsVerifyRequest;
//import com.smarthome.domain.app.response.account.AccountBaseInfo;
//import com.smarthome.domain.app.response.account.AccountLoginInfo;
//import com.smarthome.domain.app.response.account.AccountVerifyToken;
//
///**
// * 账户业务服务。
// *
// */
//@Service("accountUpdateService")
//public class AccountUpdateServiceImpl implements AccountUpdateService {
//
//    protected static Logger LOGGER;
//
//    @Resource
//    private ShTestAccountService shTestAccountService;
//    @Resource
//    private AccountVerifyTokenService accountVerifyTokenService;
//
//    public AccountUpdateServiceImpl() {
//        LOGGER = LoggerFactory.getLogger(this.getClass());
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public AccountBaseInfo register(AccountRegisterRequest request) {
//        String mobile = request.getMobile();
//        String token = request.getToken();
//        accountVerifyTokenService.validActionToken(token, mobile);// 验证token
//
//        ShTestAccount shTestAccount = new ShTestAccount();
//        BeanUtils.copyProperties(request, shTestAccount);
//        shTestAccount.setStatus(1);
//        shTestAccount.setGmtCreate(DateUtils.getCurrentDate());
//        shTestAccount.setGmtModify(shTestAccount.getGmtCreate());
//        if (StringUtils.isNotBlank(shTestAccount.getLoginPwd()))
//            shTestAccount.setLoginPwd(MD5Utils.encode(shTestAccount.getLoginPwd()));
//        // 生成账户基本信息
//        if (shTestAccountService.insert(shTestAccount) == 1) {
//        } else {
//            LOGGER.info("资金账户创建失败");
//            ExceptionUtils.throwBaseException(ResponseCode.ACCOUNT_REGISTER_FAILURE);
//        }
//        AccountBaseInfo baseInfo = new AccountBaseInfo();
//        BeanUtils.copyProperties(shTestAccount, baseInfo);
//        // 删除token
//        accountVerifyTokenService.deleteActionToken(request.getToken());
//        return baseInfo;
//    }
//
//    @Override
//    public AccountLoginInfo login(AccountLoginRequest request) {
//        ShTestAccount shTestAccount = null;
//        if (request.getLoginType() == 1) {// 用户名登录
//            shTestAccount = shTestAccountService.getAccountByLoginName(request.getLoginName());
//        } else if (request.getLoginType() == 2) {// 手机号登录
//            shTestAccount = shTestAccountService.getAccountByMobile(request.getLoginName());
//        } else if (request.getLoginType() == 3) {// email登录
//            shTestAccount = shTestAccountService.getAccountByEmail(request.getLoginName());
//        }
//        if (shTestAccount == null) {
//            ExceptionUtils.throwBaseException(ResponseCode.ACCOUNT_NOT_EXIST);
//        }
//        // 验证密码
//        String dbPwd = shTestAccount.getLoginPwd();
//        String reqPwd = MD5Utils.encode(request.getLoginPwd());
//        if (!StringUtils.equals(dbPwd, reqPwd)) {
//            ExceptionUtils.throwBaseException(ResponseCode.LOGIN_PWD_ERROR);
//        }
//        // 组装返回数据
//        AccountLoginInfo accountLoginInfo = new AccountLoginInfo();
//        BeanUtils.copyProperties(shTestAccount, accountLoginInfo);
//        accountLoginInfo.setAccountId(shTestAccount.getId());
//        accountLoginInfo.setLoginTime(DateUtils.getCurrentDate());
//        accountLoginInfo.setLoginToken(initAndSaveToken(accountLoginInfo));
//        return accountLoginInfo;
//    }
//
//    @Override
//    public AccountLoginInfo autoLogin(Request request) {
//        AccountLoginInfo context = request.getAccountLoginInfo();
//        if (null == context || context.getAccountId() == null) {
//            ExceptionUtils.throwCommonException(ResponseCode.ACCOUNT_NOT_LOGIN);
//        }
//        ShTestAccount shTestAccount = shTestAccountService.selectEntryById(context.getAccountId());
//        String newToken = initAndSaveToken(context);// 生成新token
//        JedisUtils.del(request.getLoginToken()); // 删除原token
//        // 组装返回数据
//        AccountLoginInfo response = new AccountLoginInfo();
//        BeanUtils.copyProperties(shTestAccount, response);
//        response.setLoginTime(DateUtils.getCurrentDate());
//        // response.setFrom(request.getFrom());
//        response.setLoginToken(newToken);
//        return response;
//
//    }
//
//    /**
//     * 初始化并缓存登录token
//     * @param context
//     * @return
//     */
//    private String initAndSaveToken(AccountLoginInfo context) {
//        String token = CacheKeys.KEY_LOGIN_TOKEN + "_" + UUID.randomUUID() + "_" + context.getAccountId() + "_" + DateUtils.format(context.getLoginTime());
//        String loginToken = MD5Utils.hash(token);
//        JedisUtils.set(loginToken, context, Constants.REDIS_KEY_LOGIN_TOKEN_EXPIRE_TIME);
//        return loginToken;
//    }
//
//    @Override
//    public void modifyHeadImage(Long accountId, String image) {
//        ShTestAccount uAccount = new ShTestAccount();
//        uAccount.setId(accountId);
//        uAccount.setHeadImage(image);
//        shTestAccountService.updateById(uAccount);
//    }
//
//    @Override
//    public void resetPassword(AccountPwdResetRequest request) {
//        String mobile = request.getMobile();
//        String token = request.getToken();
//        AccountVerifyToken accountVerify = accountVerifyTokenService.validActionToken(token, mobile);
//        ShTestAccount shTestAccount = shTestAccountService.selectEntryById(accountVerify.getAccountId());
//        if (null == shTestAccount) {
//            ExceptionUtils.throwCommonException(ResponseCode.ACCOUNT_NOT_EXIST);
//        }
//        if (!request.getAccountId().equals(accountVerify.getAccountId())) {
//            ExceptionUtils.throwCommonException(ResponseCode.NOT_NORMAL_ACCESS, "非法访问, accountId error");
//        }
//        ShTestAccount uAccount = new ShTestAccount();
//        uAccount.setId(shTestAccount.getId());
//        uAccount.setLoginPwd(MD5Utils.encode(request.getPwdNew()));
//        shTestAccountService.updateById(uAccount);
//        accountVerifyTokenService.deleteActionToken(token);
//    }
//
//    @Override
//    public String resetPasswordVerifyCard(AccountPwdResetVerifyCardRequest request) {
//        String mobile = request.getMobile();
//        String token = request.getToken();
//        // 验证token
//        AccountVerifyToken accountVerify = accountVerifyTokenService.validActionToken(token, mobile);
//
//        if (accountVerify.getBizType() != VerifyTokenType.SMS_FIND_LOGIN_PWD) {
//            ExceptionUtils.throwCommonException(ResponseCode.NOT_NORMAL_ACCESS);
//        }
//        if (request.getAccountId() != accountVerify.getAccountId().longValue()) {
//            ExceptionUtils.throwCommonException(ResponseCode.NOT_NORMAL_ACCESS);
//        }
//        ShTestAccount shTestAccount = shTestAccountService.getAccountByMobile(mobile);
//        // 验证实名认证信息
//        if (null == shTestAccount) {
//            ExceptionUtils.throwCommonException(ResponseCode.ACCOUNT_NOT_EXIST);
//        }
//        if (shTestAccount.getCertStatus() != CertStatus.CERTIFIED.getType()) {
//            ExceptionUtils.throwCommonException(ResponseCode.ACCOUNT_NOT_CERTIFICATE);
//        }
//        if (!StringUtils.equals(request.getIdCardNo(), shTestAccount.getCertNo())) {
//            ExceptionUtils.throwCommonException(ResponseCode.CREDENTIAL_NO_ERROR);
//        }
//        String newToken = accountVerifyTokenService.generateNewActionToken(shTestAccount.getId(), mobile, accountVerify.getBizType());
//        accountVerifyTokenService.deleteActionToken(token);
//        return newToken;
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void modifyMobile(AccountMobileModifyRequest request) {
//        AccountLoginInfo context = request.getAccountLoginInfo();
//        String token = request.getToken();
//        String newMobile = request.getMobile();
//        String oldMobile = context.getMobile();
//        String smsVerifyCode = request.getVerifyCode();
//        Long accountId = context.getAccountId();
//        if (StringUtils.equals(newMobile, oldMobile)) {
//            ExceptionUtils.throwCommonException(ResponseCode.MOBILE_REPEAT);
//        }
//        // 验证原手机号动态验证码通过的token
//        accountVerifyTokenService.validActionToken(token, oldMobile);
//        // 验证新手机验证码
//        SmsVerifyRequest sRequest = new SmsVerifyRequest();
//        BeanUtils.copyProperties(request, sRequest);
//        sRequest.setAccountId(accountId);
//        sRequest.setBizType(VerifyTokenType.SMS_MOBILE_VERIFY.getType());
//        sRequest.setVerifyCode(smsVerifyCode);
//        sRequest.setMobile(newMobile);
//        String newToken = accountVerifyTokenService.consumeSmsVerifyCode(sRequest);
//        // 修改手机号
//        ShTestAccount uAccount = new ShTestAccount();
//        uAccount.setId(accountId);
//        uAccount.setMobile(newMobile);
//        shTestAccountService.updateById(uAccount);
//        // 删除token
//        accountVerifyTokenService.deleteActionToken(token, newToken);
//    }
//
//}
